package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.Result;
import com.lsyw.intergrate.entity.JcFactor;
import com.lsyw.intergrate.entity.JcMassifFactor;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.lsyw.intergrate.service.JcFactorService;
import com.lsyw.intergrate.service.JcMassifFactorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 地块的监测因子 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2023-12-11
 */
@Api(tags = "地块的监测因子")
@RestController
@RequestMapping("/jc-massif-factor")
public class JcMassifFactorController {

    @Autowired
    private JcMassifFactorService jcMassifFactorService;

    @Autowired
    private JcFactorService jcFactorService;
    /**
     * 通过地块id查询
     */
    @GetMapping("/get-by-massif-id")
    @ApiOperation(value = "通过地块id查询", notes = "通过地块id查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "massifId", value = "massifId", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "devId", value = "devId", required = true, paramType = "query", dataType = "String")
    })
    public Result getByMassifId(String massifId,String devId) {
        LambdaQueryWrapper<JcMassifFactor> lambda = new QueryWrapper<JcMassifFactor>().lambda();
        lambda.eq(JcMassifFactor::getDelFlag, 0).eq(JcMassifFactor::getMassifId, massifId).eq(JcMassifFactor::getDeviceId,devId);
        List<JcMassifFactor> list = jcMassifFactorService.list(lambda);
        for (int i = 0; i < list.size(); i++) {
            String jcFactorId = list.get(i).getJcFactorId();
            JcFactor jcFactor = this.jcFactorService.getById(jcFactorId);
            list.get(i).setUnitMeasurement(jcFactor.getUnitMeasurement());
        }
        return Result.successResult(list);
    }


    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    public Result getById(String id) {
        return Result.successResult(jcMassifFactorService.getById(id));
    }

    /**
     * 新增(根据地块设置监测因子项)
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增(根据地块设置监测因子项)", notes = "新增(根据地块设置监测因子项)", httpMethod = "POST")
    public Result save(@RequestBody List<JcMassifFactor> jcMassifFactors) {
        if(jcMassifFactors==null || jcMassifFactors.size()==0){
            return Result.errorResult(null,"数据不全");
        }else {
            JcMassifFactor jcMassifFactor = jcMassifFactors.get(0);
            String massifId = jcMassifFactor.getMassifId();
            String deviceId = jcMassifFactor.getDeviceId();
            LambdaQueryWrapper<JcMassifFactor> lambda = new QueryWrapper<JcMassifFactor>().lambda();
            lambda.eq(JcMassifFactor::getDelFlag,0).eq(JcMassifFactor::getMassifId,massifId).eq(JcMassifFactor::getDeviceId,deviceId);
            List<JcMassifFactor> list = this.jcMassifFactorService.list(lambda);
            for (JcMassifFactor jmf: list) {
                jmf.setDelFlag(1);
                this.jcMassifFactorService.updateById(jmf);
            }
        }
        for (JcMassifFactor jcMassifFactor: jcMassifFactors) {
            jcMassifFactor.setDelFlag(0);
            jcMassifFactor.setGmtCreate(new Date());
            jcMassifFactorService.save(jcMassifFactor);
        }
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id/{id}")
    public Result delete(@PathVariable(value = "id") String ids) {
        String[] idsStrs = ids.split(",");
        for (String id : idsStrs) {
            jcMassifFactorService.removeById(Long.parseLong(id));
        }
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result updateById(@RequestBody JcMassifFactor jcMassifFactor) {
        jcMassifFactorService.updateById(jcMassifFactor);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody JcMassifFactorReqVo jcMassifFactor) {
        final LambdaQueryWrapper<JcMassifFactor> lambda = new QueryWrapper<JcMassifFactor>().lambda();
        this.buildCondition(lambda, jcMassifFactor);
        return Result.successResult(jcMassifFactorService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<JcMassifFactor> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<JcMassifFactor> lambda = new QueryWrapper<JcMassifFactor>().lambda();
        //TODO 自己的查询逻辑
        return Result.successResult(this.jcMassifFactorService.page(page, lambda));
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<JcMassifFactor> lambda, JcMassifFactorReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(JcMassifFactor::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getMassifId())) {
            lambda.eq(JcMassifFactor::getMassifId, param.getMassifId());
        }
        if (!StringUtils.isEmpty(param.getJcFactorId())) {
            lambda.eq(JcMassifFactor::getJcFactorId, param.getJcFactorId());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(JcMassifFactor::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(JcMassifFactor::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(JcMassifFactor::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(JcMassifFactor::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(JcMassifFactor::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(JcMassifFactor::getGmtModified, param.getGmtModifiedList().get(1));
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(JcMassifFactor::getDelFlag, param.getDelFlag());
        }
        if (!StringUtils.isEmpty(param.getName())) {
            lambda.eq(JcMassifFactor::getName, param.getName());
        }
        lambda.orderBy(true, false, JcMassifFactor::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class JcMassifFactorReqVo extends JcMassifFactor {
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
    }


}
